package com.htandroid.htandroid.aty;

/**
 * Created by Administrator on 2015/12/26.
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.GcwxList;
import com.htandroid.htandroid.entity.GcwxVouchsForeign;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.tools.ToolLog;

import java.util.ArrayList;

public class Gcwx_foreign extends AbActivity {
    private GcwxList vouch;
    private GcwxVouchsForeign vouchforeign;
    private ArrayList<GcwxVouchsForeign> vouchsforeignlist;
    private String listtype = "0";
    private String ctype = "xd";
    private int icurrentrow;

    private AbTitleBar mAbTitleBar = null;
    private View rightView1 = null;
    private View rightView2 = null;
    private View rightView3 = null;
    private TextView tv_currentrow;
    private AbLoadDialogFragment mDialogFragment = null;

    @AbIocView(id = R.id.iquantity_et)
    EditText iquantity_et;
    @AbIocView(id = R.id.iprice_et)
    EditText iprice_et;
    @AbIocView(id = R.id.cinvname_tv)
    EditText cinvname_tv;
    @AbIocView(id = R.id.cinvstd_tv)
    EditText cinvstd_tv;
    @AbIocView(id = R.id.cinvunit_tv)
    EditText cinvunit_tv;
    @AbIocView(id = R.id.cmemo_et)
    EditText cmemo_et;
    @AbIocView(id = R.id.iamount_tv)
    TextView iamount_tv;
    @AbIocView(id = R.id.btn1_tv, click = "btnClick")
    TextView btn1_tv;
    @AbIocView(id = R.id.btn2_tv, click = "btnClick")
    TextView btn2_tv;
    @AbIocView(id = R.id.btn3_tv, click = "btnClick")
    TextView btn3_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_gcwx_foreign);
        initview();
        //已有材料显示,没有则默认增加一行并显示
        if (vouchsforeignlist != null && vouchsforeignlist.size() > 0) {
            insert_data(icurrentrow);
        } else {
            //增加空行
            GcwxVouchsForeign ivouchforeign = new GcwxVouchsForeign();
            //初始数据把所有列置空
            ivouchforeign.init(vouch.getCvouchid());
            vouchsforeignlist.add(ivouchforeign);
            insert_data(vouchsforeignlist.size() - 1);
        }
    }

    public void btnClick(View v) {
        switch (v.getId()) {
            case R.id.btn1_tv:
                btn1_click(v);
                break;
            case R.id.btn2_tv:
                btn2_click(v);
                break;
            case R.id.btn3_tv:
                btn3_click(v);
                break;
        }
    }

    //初始化
    private void initview() {
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("外包材料");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.setTitlePadding(118, 0, 0, 0);

        mAbTitleBar.clearRightView();
        rightView1 = mInflater.inflate(R.layout.btn_abs_back, null);
        mAbTitleBar.addRightView(rightView1);
        rightView2 = mInflater.inflate(R.layout.tv_currentrow, null);
        mAbTitleBar.addRightView(rightView2);
        rightView3 = mInflater.inflate(R.layout.btn_abs_forward, null);
        mAbTitleBar.addRightView(rightView3);
        tv_currentrow = (TextView) rightView2.findViewById(R.id.tv_currentrow);
        tv_currentrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                more_list(v);
            }
        });

        //获取传递的对象参数
        Intent intent = getIntent();
        vouchsforeignlist = (ArrayList<GcwxVouchsForeign>) intent.getSerializableExtra("vouchsforeignlist");
        vouch = (GcwxList) intent.getSerializableExtra("vouch");
        listtype = intent.getExtras().getString("listtype");
        ctype = intent.getExtras().getString("ctype");
        icurrentrow = getIntent().getExtras().getInt("currentrow");

        //通过数量修改的监听
        iquantity_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
                return false;
            }
        });

        //通过数量焦点变化
        iquantity_et.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
            }
        });

        //单价修改的监听
        iprice_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
                return false;
            }
        });

        //单价焦点变化
        iprice_et.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
            }
        });
    }


    //为窗口填充数据
    public void insert_data(int row) {
        vouchforeign = vouchsforeignlist.get(row);
        icurrentrow = row;
        tv_currentrow.setText((icurrentrow + 1) + "/" + (vouchsforeignlist.size()));
        //增加/删除按钮
        if (listtype.equals("0")) {
            showwin(true);
        } else {
            showwin(false);
        }
        //写入数据
        iquantity_et.setText(Basenvo.customFormat("######.00##", vouchforeign.getIquantity()));
        iprice_et.setText(Basenvo.customFormat("######.00##", vouchforeign.getIprice()));

        cinvname_tv.setText(vouchforeign.getCinvname());
        cinvstd_tv.setText(vouchforeign.getCinvstd());
        cinvunit_tv.setText(vouchforeign.getCinvm_uint());
        iamount_tv.setText(Basenvo.customFormat("######.00##", vouchforeign.getIamount()));
        cmemo_et.setText(vouchforeign.getCmemo());
    }

    public void showwin(boolean stype) {
        //背景变白，字变黑色
        Integer backgroundcolor = Color.TRANSPARENT;
        if (ctype.equals("check") && stype) {
            stype = false;
        }
        iquantity_et.setEnabled(stype);
        iprice_et.setEnabled(stype);
        cinvname_tv.setEnabled(stype);
        cinvunit_tv.setEnabled(stype);
        cinvstd_tv.setEnabled(stype);
        cmemo_et.setEnabled(stype);
        if (stype) {
            iquantity_et.setBackgroundResource(R.drawable.edit_background);
            iprice_et.setBackgroundResource(R.drawable.edit_background);
            cinvname_tv.setBackgroundResource(R.drawable.edit_background);
            cinvunit_tv.setBackgroundResource(R.drawable.edit_background);
            cinvstd_tv.setBackgroundResource(R.drawable.edit_background);
            cmemo_et.setBackgroundResource(R.drawable.edit_background);
            //隐藏修改,删除确定
            btn1_tv.setVisibility(View.VISIBLE);
            btn2_tv.setVisibility(View.VISIBLE);
            btn3_tv.setVisibility(View.VISIBLE);
        } else {
            iquantity_et.setBackgroundColor(backgroundcolor);
            iprice_et.setBackgroundColor(backgroundcolor);
            cinvname_tv.setBackgroundColor(backgroundcolor);
            cinvunit_tv.setBackgroundColor(backgroundcolor);
            cinvstd_tv.setBackgroundColor(backgroundcolor);
            cmemo_et.setBackgroundColor(backgroundcolor);
            //隐藏修改,删除确定
            btn1_tv.setVisibility(View.GONE);
            btn2_tv.setVisibility(View.GONE);
            btn3_tv.setVisibility(View.GONE);
        }
    }

    //存货切换
    public void more_list(View v) {
        updatedataref();
        if (!prechecked()) return;
        Intent intent = new Intent(getApplicationContext(), Gcwx_foreign_detaillist.class);
        //传递参数
        Bundle bundle = new Bundle();
        intent.putExtra("currentrow", "" + icurrentrow);//当前行号
        bundle.putSerializable("vouchsforeignlist", vouchsforeignlist);//存货明细列表
        intent.putExtras(bundle);
        startActivityForResult(intent, R.layout.layout_gcwx_inventory_detaillist / 500000);
    }

    //新增空行
    public void btn1_click(View v) {
        updatedataref();
        if (!prechecked()) return;
        GcwxVouchsForeign ivouchsforeign = new GcwxVouchsForeign();
        //初始数据把所有列置空
        ivouchsforeign.init(vouch.getCvouchid());
        vouchsforeignlist.add(ivouchsforeign);
        insert_data(vouchsforeignlist.size() - 1);
    }

    //删除当前行
    public void btn2_click(View v) {
        vouchsforeignlist.remove(icurrentrow);
        //最后一行删除则需要顺序上移
        if (icurrentrow > vouchsforeignlist.size() - 1) {
            //如果已经删完数据,则添加一个空行数据
            if (vouchsforeignlist.size() == 0) {
                GcwxVouchsForeign ivouchsforeign = new GcwxVouchsForeign();
                //初始数据把所有列置空
                ivouchsforeign.init(vouch.getCvouchid());
                vouchsforeignlist.add(ivouchsforeign);
            }
            insert_data(vouchsforeignlist.size() - 1);
        } else {
            insert_data(icurrentrow);
        }
    }

    //确定返回上一页面
    public void btn3_click(View v) {
        updatedataref();
        if (!prechecked()) return;
        Intent intent = new Intent(getApplicationContext(), Gcwx_check_detail.class);
        //传递参数
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchsforeignlist", vouchsforeignlist);
        intent.putExtras(bundle);
        setResult(R.layout.layout_gcwx_foreign / 300000, intent);  //设置返回结果
        //打开延缓窗口,关闭当前窗口
        this.finish();
    }

    //对一些修改后的值进行处理,避免因为没触发事件导致保存的信息不正确
    public void updatedataref() {
        String iquantity = iquantity_et.getText().toString();
        String iprice = iprice_et.getText().toString();
        if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
            iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
        } else {
            iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
        }
        //保存修改内容
        if (iquantity_et.getText() != null) {
            vouchforeign.setIquantity(iquantity_et.getText().toString());
        }
        if (iprice_et.getText() != null) {
            vouchforeign.setIprice(iprice_et.getText().toString());
        }
        if (iamount_tv.getText() != null) {
            vouchforeign.setIamount(iamount_tv.getText().toString());
        }
        if (cinvname_tv.getText() != null) {
            vouchforeign.setCinvname(cinvname_tv.getText().toString());
        }
        if (cinvstd_tv.getText() != null) {
            vouchforeign.setCinvstd(cinvstd_tv.getText().toString());
        }
        if (cinvunit_tv.getText() != null) {
            vouchforeign.setCinvm_uint(cinvunit_tv.getText().toString());
        }
        if (cmemo_et.getText() != null) {
            vouchforeign.setCmemo(cmemo_et.getText().toString());
        }
    }

    //检测数据是否合法
    public boolean prechecked() {
        return true;
    }

    //向左翻页
    public void btn_abs_back(View v) {
        updatedataref();
        if (!prechecked()) return;
        if (icurrentrow == 0) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是第一项!");
        } else {
            insert_data(icurrentrow - 1);
        }
    }

    //向右翻页
    public void btn_abs_forward(View v) {
        updatedataref();
        if (!prechecked()) return;
        if (icurrentrow == (vouchsforeignlist.size() - 1)) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是最后一项!");
        } else {
            insert_data(icurrentrow + 1);
        }
    }

    //接收返回数据(存货明细,行切换,成本对象)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.layout_gcwx_inventory_detaillist / 500000:
                Bundle bundle = data.getExtras();
                String listrow = bundle.getString("listrow");
                insert_data(Integer.valueOf(listrow));
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

